/**
 * @file STL_CURL.h
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2021-01-08
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2021
 * 
 */

#ifndef __STL_CURL_H__
#define __STL_CURL_H__

#include <curl/curl.h>
// #include "STL_Json.h"
#include "STL_ModLog.h"
#include <string>

namespace STL
{
    class STL_CURL : public STL_ModLog
    {
    public:
        typedef struct CurlMemory_t
        {
            char *response = nullptr;
            size_t size = 0;
        } CurlMemory;

        typedef struct HttpReqest_t
        {
            std::string url = "";      // 请求连接
            std::string body = "";     // 请求消息体（POST专用）
            int timeout = 10;          // 超时时间
            bool isLog = true;         //日志输出
            std::string codeFile = ""; // 日志输出
            int codeLine = 0;          // 日志输出
            HttpReqest_t()
            {
            }
            HttpReqest_t(std::string url, int timeout, std::string codeFile, int codeLine, bool isLog = true)
            {
                this->url = url;
                this->timeout = timeout;
                this->codeFile = codeFile;
                this->codeLine = codeLine;
                this->isLog = isLog;
            }
        } HttpReqest;

        STL_SYNTHESIZE(std::string, m_dns, DNS);

    private:
        STL_CURL();

    public:
        static STL_CURL *getInstance();

    private:
        static size_t write_function_callback(void *data, size_t size, size_t nmemb, void *userp);

    public:
        std::string stl_http_get(HttpReqest req);
        std::string stl_http_get(std::string url, std::string codeFile, int codeLine, bool isLog = true);
        std::string stl_http_post(HttpReqest req);
        std::string stl_http_post(std::string url, std::string body, std::string codeFile, int codeLine, bool isLog = true);
    };
} // namespace STL

#endif /* __STL_CURL_H__ */